package com.ansuenjob.util;

import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;


public class DES {
	
	public static final Charset UTF_8 = Charset.forName("UTF-8");
	public static final Charset GBK = Charset.forName("GBK");

	private static final String CIPHER_ALG_1 = "DES/CBC/PKCS5Padding";
//	private static final String CIPHER_ALG_1 = "DES";
	private static String encodeStr;
	private static String decodeStr;

	public static byte[] enctypt(String source) throws Exception {
		return enctypt2byte(source, encodeStr.getBytes(UTF_8));
	}

	public static byte[] decrypt(byte[] secData) throws Exception {
		return decrypt(secData, decodeStr.getBytes(UTF_8));
	}
	
	public static String decrypt2string(byte[] secData) throws Exception {
		return new String(decrypt(secData), UTF_8);
	}

	/**
	 * 
	 * 
	 * @param source
	 * @param byteKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] enctypt2byte(String source, byte[] byteKey)
			throws Exception {
		Key key = genKey(byteKey);
		Cipher cipher = Cipher.getInstance(CIPHER_ALG_1);
		IvParameterSpec iv = new IvParameterSpec(encodeStr.getBytes("UTF-8"));
		cipher.init(Cipher.ENCRYPT_MODE, key, iv);
		return cipher.doFinal(source.getBytes(UTF_8));
	}

	/**
	 * 
	 * 
	 * @param secData
	 * @param byteKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] decrypt(byte[] secData, byte[] byteKey)
			throws Exception {
		Key key = genKey(byteKey);
		Cipher cipher = Cipher.getInstance(CIPHER_ALG_1);
		IvParameterSpec iv = new IvParameterSpec(decodeStr.getBytes("UTF-8"));
		cipher.init(Cipher.DECRYPT_MODE, key, iv);
		return cipher.doFinal(secData);
	}

	/**
	 * 
	 * 
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] genByteKeyWithStr(String str)
			throws NoSuchAlgorithmException {
		KeyGenerator keyGen = KeyGenerator.getInstance(CIPHER_ALG_1);
		keyGen.init(new SecureRandom(str.getBytes(UTF_8)));
		SecretKey key = keyGen.generateKey();
		return key.getEncoded();
	}

	/**
	 * 
	 * 
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
	public static byte[] genByteKey() throws NoSuchAlgorithmException {
		KeyGenerator keyGen = KeyGenerator.getInstance("DES");
		keyGen.init(new SecureRandom());
		SecretKey key = keyGen.generateKey();
		return key.getEncoded();
	}

	/**
	 * 
	 * 
	 * @param byteKey
	 * @return
	 * @throws InvalidKeyException
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
	private static Key genKey(byte[] byteKey) throws InvalidKeyException,
			NoSuchAlgorithmException, InvalidKeySpecException {
		DESKeySpec keyS = new DESKeySpec(byteKey);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey key = keyFactory.generateSecret(keyS);
		return key;
	}
	
	public static void initStr(String desStr) {
		encodeStr = desStr.substring(0, 8);
		decodeStr = desStr.substring(0, 8);
	}
	
	public static String getKeyStr() {
		return encodeStr;
	}
}
